<template>
    <div class="app-container tz-app">
        摄像头11
        <Jessibuca></Jessibuca>
        <div class="zt-select flex-s">
            <div class="flex-left">
              
                <!-- <el-select v-model="queryParams.cameraName" filterable placeholder="节点" class="zt-input rihgt-m5" >
                    <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label">
                    </el-option>
                </el-select>

                <el-select v-model="queryParams.cameraName" filterable placeholder="机房" class="zt-input-min rihgt-m5">
                    <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label">
                    </el-option>
                </el-select>

                <button class="bt-blue " @click="handleQuery">筛选</button> -->

            </div>
            <button class="bt-yellow " @click="handleAdd">新增</button>
        </div>

        <div>
            
        </div>
        <div class="tz-table-box" style="height:calc(100vh - 250px);">
            <el-table v-loading="loading" :data="cameraList" @selection-change="handleSelectionChange"
                height="calc(100vh - 250px)" class="zt-table">
                <!-- <el-table-column label="摄像头ID" align="center" prop="cameraId" /> -->
                <el-table-column label="摄像头名称" align="center" prop="cameraName" />
                <el-table-column label="创建时间" align="center" prop="createTime" width="190" />
                <el-table-column label="所属地址" align="center" prop="locatedAddress">
                    <template slot-scope="scope">
                        {{ getLocatedAddress(scope.row.locatedAddress) }}
                    </template>
                </el-table-column>
                <!-- <el-table-column label="摄像头类型" align="center" prop="cameraType" /> -->
                <!-- <el-table-column label="摄像头属性" align="center" prop="cameraAttribute" show-overflow-tooltip /> -->
                <el-table-column label="所属机房" align="center" prop="cameraAttribute" show-overflow-tooltip><template
                        slot-scope="scope">-</template></el-table-column>
                <el-table-column label="所属机箱" align="center" prop="cameraAttribute" show-overflow-tooltip><template
                        slot-scope="scope">-</template></el-table-column>
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        {{ scope.row.status == 0 ? '正常' : scope.row.status == 2 ? '离线' : '异常' }}
                    </template>
                </el-table-column>
                <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <div class="flex-s camera-scope">
                            <img src="@/assets/images/amend.png"
                                style="width: 20px;height: 20px;cursor: pointer;margin-right: 20px;"
                                @click="handleUpdate(scope.row)" title="编辑" />
                            <img src="@/assets/images/delete-bin2.png" style="width: 20px;height: 20px;cursor: pointer;"
                                @click="handleDelete(scope.row)" title="删除" />

                            <el-popover placement="left-end" :width="150" trigger="click" :visible-arrow="false">
                                <div slot="reference" class="result flex-center">
                                    <div class="flv-player-button" @click="flvButton(scope.row)">
                                        <img src="@/assets/images/wi.png"
                                            style="width: 20px;height: 20px;cursor: pointer;margin-left: 20px;" title="预览" />
                                    </div>
                                </div>
                                <div slot="default" class="flv-player-popover" > 
                                    
                                    <iframe :src="scope.row.cameraUrl" style="width: 400px; height: 250px;"></iframe>
                                    <!-- <flv-player  controls
                                        autoplay :muted="true" :source="flv.src" :type="flv.type" /> -->

                                         <!-- <iframe :src="websiteUrl" style="width: 400px; height: 250px;"></iframe> -->
                                        <!-- {{ scope.row.cameraUrl }} -->
                                </div>

                            </el-popover>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>


        <div class="zt-pagination">
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>

        <!-- 添加或修改摄像头对话框 -->
        <right-dialog :title="title" :visible="open" @closeDialog="cancel">
            <template v-slot:default>
                <div class="zt-dialog-solt">
                    <el-form ref="form" :model="form" :rules="rules" label-width="125px">
                        <el-form-item label="摄像头名称:" prop="cameraName">
                            <div class="flex">
                                <el-input class="blue-input" v-model="form.cameraName" placeholder="请输入摄像头名称" />
                                <el-button class="bt-blue " @click="submitForm" style="margin-left: 10px; ">确定</el-button>
                            </div>
                        </el-form-item>

                        <el-form-item label="所属地:" prop="locatedAddress">
                            <div class="flex">
                                <el-select v-model="form.locatedAddress" filterable placeholder="所属地"
                                    class="zt-input-min rihgt-m5" style="width: 120px;font-weight: 600;">
                                    <el-option v-for="item in  dict.type.located_address" :key="item.value"
                                        :label="item.label" :value="item.value" />
                                </el-select>

                                <el-form-item label="摄像头类型:" prop="inoutType">
                                    <el-select v-model="form.inoutType" filterable placeholder="摄像头类型"
                                        class="zt-input-min rihgt-m5" style="width: 120px;font-weight: 600;">
                                        <el-option v-for="item in  dict.type.inout_type" :key="item.value"
                                            :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>

                            </div>
                        </el-form-item>


                        <el-form-item label="云曜项目ID:" prop="projectId">
                            <div class="flex">
                                <el-input v-model="form.projectId" filterable placeholder="云曜项目ID"
                                    class="zt-input-min rihgt-m5" style="width: 149px;font-weight: 600;">
                                </el-input>


                                <el-form-item label="云曜设备序列号:" prop="deviceSerial">
                                    <el-input v-model="form.deviceSerial" filterable placeholder="AB7723774"
                                        @input="handleInput" :maxlength="9" ref="addressInput" class="zt-input-min rihgt-m5"
                                        style="width: 120px;font-weight: 600;">
                                    </el-input>
                                </el-form-item>

                                <el-form-item label="设备通道:">
                                    <el-input v-model="form.channelNo" filterable placeholder="设备通道"
                                       :maxlength="9" ref="addressInput" class="zt-input-min rihgt-m5"
                                        style="width: 120px;font-weight: 600;">
                                    </el-input>
                                </el-form-item>

                            </div>
                        </el-form-item>

                        <el-form-item label="摄像头流地址:">
                            <el-input v-model="form.cameraUrl" type="textarea" placeholder="摄像头流地址" />
                        </el-form-item>
                        <el-form-item label="备注:">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-form>

                </div>
            </template>
        </right-dialog>
    </div>
</template>
  
<script>
import { listCamera, getCamera, delCamera, addCamera, updateCamera } from "@/api/business/camera";
import RightDialog from "@/components/TZ/RightDialog"
import TSelect from "@/components/TZ/TSelect"
import flvPlayer from "@/components/TZ/CameraVideo/index.js";
import Jessibuca from "@/components/TZ/Jessibuca";

export default {
    components: { RightDialog, TSelect, flvPlayer , Jessibuca },
    dicts: ['located_address', 'inout_type'],
    name: "Camera",
    data() {
        return {
            websiteUrl:'https://open.ys7.com/jssdk/theme.html?url=ezopen://open.ys7.com/G61959432/1.hd.live&accessToken=ar.6pzwfiwj9hm97boxc3d86hpsb7n21ht6-86tepxwszj-1kcldkx-ie6pnz0yv&id=ysopen',
            type: '',
            src: '',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 摄像头表格数据
            cameraList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                cameraName: null,
                cameraType: null,
                cameraAttribute: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                cameraName: [
                    { required: true, message: "摄像头名称不能为空", trigger: "blur" }
                ],
                cameraType: [
                    { required: true, message: "摄像头类型不能为空", trigger: "change" }
                ],
                cameraAttribute: [
                    { required: true, message: "摄像头属性不能为空", trigger: "blur" }
                ],
                cameraUrl: [
                    { required: true, message: "摄像头流地址能为空", trigger: "blur" }
                ],
                deviceSerial: [
                    { required: true, message: "设备序列号不能为空", trigger: "blur" }
                ],
                projectId: [
                    { required: true, message: "云曜项目ID不能为空", trigger: "blur" }
                ],
                locatedAddress: [
                    { required: true, message: "所属地不能为空", trigger: "blur" }
                ],
                status: [ 
                    { required: true, message: "状态不能为空", trigger: "change" }
                ],
                inoutType: [
                    { required: true, message: "摄像头类型不能为空", trigger: "change" }
                ],
            },
            options: [{
                value: '1',
                label: '1'
            }, {
                value: '天线',
                label: '天线'
            }, {
                value: '2',
                label: '2'
            }, {
                value: '3',
                label: '3'
            }, {
                value: '天下七七',
                label: '天下七七'
            }],
            flv: {
                type: 'flv',
                src: '',
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        flvButton(row) {
            this.flv.src = row.cameraUrl
        },
        handleInput(value) {
            // 移除非法字符
            const formattedValue = value.replace(/[^A-Za-z0-9]/g, '');
            // 限制长度
            if (formattedValue.length > 9) {
                this.form.deviceSerial = formattedValue.substring(0, 9);
            } else {
                this.form.deviceSerial = formattedValue;
            }
        },
        /** 查询摄像头列表 */
        getList() {
            this.loading = true;
            listCamera(this.queryParams).then(response => {
                this.cameraList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                cameraId: null,
                cameraName: null,
                cameraType: null,
                cameraAttribute: null,
                locatedAddress: null,
                isUsed: null,
                status: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                inoutType: null,
                cameraUrl: null,
                projectId: null,
                deviceSerial: null,
                channelNo:null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.cameraId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加摄像头";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const cameraId = row.cameraId || this.ids
            getCamera(cameraId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改摄像头";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.cameraId != null) {
                        updateCamera(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCamera(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const cameraIds = row.cameraId || this.ids;
            this.$modal.confirm('是否确认删除摄像头编号为"' + cameraIds + '"的数据项？').then(function () {
                return delCamera(cameraIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/camera/export', {
                ...this.queryParams
            }, `camera_${new Date().getTime()}.xlsx`)
        },
        getLocatedAddress(index) {
            const entries = this.dict.type.located_address;
            for (let i = 0; i < entries.length; i++) {
                if (entries[i].value == index) {
                    return entries[i].label
                }
            }
            return ""
        },
    }
};
</script>
  